﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XiaoYuStock.Model.Models;
using System.IO;
using XiaoYuStock.AppService.Storage;
using XiaoYuStock.Model.Entities;
namespace XiaoYuStock.AppService.State
{

    /// <summary>
    /// 统计情况基类
    /// </summary>
    public abstract class AnalysedState
    {

        /// <summary>
        /// 分析的天数
        /// </summary>
        public int Days { get; set; }


        /// <summary>
        /// 结果数据
        /// </summary>
        public List<string> ResultData { get; set; }

        public abstract string StateName { get; }


        public IStorage Storage { get; set; }

        public string FileName { get {

            string stmp = System.Reflection.Assembly.GetExecutingAssembly().Location;
            stmp = stmp.Substring(0, stmp.LastIndexOf('\\'));
            stmp = Path.Combine(stmp, "data", DateTime.Now.ToString("yyyy-MM-dd"));
            if (!Directory.Exists(stmp))
            {
                Directory.CreateDirectory(stmp);
            }
            return Path.Combine(stmp, this.StateName + "_" + this.Days + ".txt");

        
        } }
        /// <summary>
        /// 分析数据
        /// </summary>
        /// <param name="historys"></param>
        public  abstract bool AnalyShareData(IEnumerable<HistoryData> historys);


        /// <summary>
        /// 分析结果
        /// </summary>
        /// <param name="shareCode"></param>
        /// <param name="historys"></param>
        public void AnalyShareResult(string shareCode, IEnumerable<HistoryData> historys)
        {
            var result = this.AnalyShareData(historys);
            if (result)
            {
                if (this.ResultData == null)
                    this.ResultData = new List<string>();
                this.ResultData.Add(shareCode);
            }
        }

     
       
       

        /// <summary>
        /// 检测数据
        /// </summary>
        /// <param name="historyData"></param>
        /// <param name="isIncrease">是否增长</param>
        /// <returns></returns>
        protected bool CheckIncrease(IEnumerable<HistoryData> historyData, bool isIncrease)
        {
            bool result = false;
            var data = historyData.OrderBy(s => s.TradeTime).ToList();
            var lastData = data.FirstOrDefault();
            if ( lastData.CloseAmout <= 0 || lastData.OpenAmount <= 0)
                return result;
            var firstRate = lastData.TradeRate??0;
            if (isIncrease)
            {
                if (firstRate < 3)
                    return result;
            }
            else
            {
                if (firstRate > -3)
                    return result;
            }
            decimal tempRate = 0;
            for (var i = 1; i < data.Count; i++)
            {
                tempRate = data[i].TradeRate ?? 0;
                if (data[i].CloseAmout <= 0 || data[i].OpenAmount <= 0)
                    return result;
                if (isIncrease)
                {
                    if (tempRate<3&&tempRate < firstRate)
                        return result;
                    else
                    {
                        firstRate = tempRate;
                    }
                }
                else
                {
                    if (tempRate > firstRate)
                        return result;
                    else
                        firstRate = tempRate;
                }

            }
            result = true;
            return result;
        }

       
    }


   


    

   

    
}
